Error Handling & Rate Limiting
This document consolidates the error handling patterns and rate limiting strategies across the two server types in the project: the Telegram Bot Server and the Webhook/REST API Server. It defines the standardized error response format, documents rate limiting policies, and explains how rate limit headers and 429 responses are handled. It also covers retry mechanisms, circuit breaker patterns, and graceful degradation strategies for robust service operation.
The error handling and rate limiting concerns are distributed across:
Servers: Telegram Bot Server and Webhook/REST API Server
Clients: Telegram API client with built-in retry/backoff and rate-limit handling
Services: Notification routing and channel-specific send operations
Core: Logging and configuration utilities
bot_server.py"] WEB["Webhook/REST API Server
webhook_server.py"] end subgraph "Clients" TGCLI["Telegram Client
telegram_client.py"] end subgraph "Services" NOTIF["Notification Service
notification_service.py"] end subgraph "Core" CFG["Config & Logging
config.py"] MAIN["CLI Entrypoint
main.py"] end BOT --> NOTIF WEB --> NOTIF NOTIF --> TGCLI BOT --> CFG WEB --> CFG MAIN --> BOT MAIN --> WEB
Diagram sources
Section sources
Standardized error response format with code, human-readable message, timestamp, and request ID.
Rate limiting policies:
Bot Commands: 30 per minute per user
REST API: 100 per minute per IP
Webhook: unlimited (trusted Telegram servers)
Rate limit headers: X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset
429 responses with retry-after guidance
Retry/backoff and exponential backoff for Telegram API calls
Circuit breaker-like behavior via graceful degradation and fallbacks
Section sources
The error handling and rate limiting architecture is layered:
Servers define endpoints and raise HTTP exceptions with standardized error bodies.
Services encapsulate cross-channel notification routing and handle per-channel failures gracefully.
Clients implement retry/backoff and translate 429 responses into controlled delays.
Logging and configuration utilities centralize logging and environment-driven behavior.
webhook_server.py" participant Service as "NotificationService
notification_service.py" participant Telegram as "TelegramClient
telegram_client.py" Client->>Webhook : "POST /api/notify" Webhook->>Service : "broadcast(message, channels)" Service->>Telegram : "send_message(...)" Telegram-->>Service : "429 Too Many Requests" Telegram->>Telegram : "Wait Retry-After seconds" Telegram-->>Service : "Success after backoff" Service-->>Webhook : "Results per channel" Webhook-->>Client : "200 OK with results"
Diagram sources
Standardized Error Response Format#
All errors follow a consistent JSON shape with code, message, timestamp, and request ID.
The API documentation defines the standard error envelope and common error codes mapped to HTTP status codes.
code, message, timestamp, request_id"] BuildErr --> ReturnErr["Return HTTP Response
with error body"] ReturnErr --> End(["Client Receives Standardized Error"])
Section sources
Rate Limiting Policies and Headers#
Bot Commands: 30 per minute per user
REST API: 100 per minute per IP
Webhook: unlimited (trusted Telegram servers)
Rate limit headers: X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset
429 responses include a retry-after hint
Section sources
Telegram Bot Server Error Handling#
Command handlers perform basic input validation and delegate to services.
Exceptions in stats calculation are caught and surfaced to the user.
Logging is used for operational visibility.
Section sources
Webhook/REST API Server Error Handling#
Endpoints raise HTTPException with standardized details for misconfiguration or processing failures.
Health endpoints return structured responses.
Stats endpoints return aggregated data or raise 501/503 when services are not configured.
webhook_server.py" participant DB as "DB Service" participant Notif as "Notification Service" Client->>API : "GET /api/stats" API->>DB : "get_placement_stats()" DB-->>API : "Data or Error" alt DB not configured API-->>Client : "501 Not Implemented" else DB available API-->>Client : "200 OK with stats" end
Section sources
Telegram Client Retry and Backoff#
The Telegram client implements retries with exponential backoff on transient failures.
On 429 responses, it reads Retry-After header and waits before retrying.
Non-transient failures are logged and retried according to backoff.
Sleep(seconds)"] --> Attempt Resp --> |Other| LogWarn["Log Warning"] --> Backoff{"More Retries?"} Backoff --> |Yes| ExpBackoff["Exponential Backoff"] --> Attempt Backoff --> |No| Fail["Return False"]
Section sources
Notification Service Failure Handling#
Broadcast loops through channels and logs errors per channel without failing the whole operation.
Unsolicited notices send path marks as sent only on full success per channel.
Section sources
Logging and Configuration#
Centralized logging configuration with environment-driven log levels and daemon mode.
Safe printing utilities suppress output in daemon mode and log instead.
Section sources
The Webhook server depends on NotificationService for routing and on TelegramClient for Telegram delivery.
The Telegram Bot Server depends on services for DB operations and stats calculation.
Logging and configuration utilities are used across servers and services.
Diagram sources
Section sources
Exponential backoff reduces thundering herds under rate limits.
Circuit breaker-like behavior: per-channel failure isolation prevents cascading failures.
Graceful degradation: continue processing other items when encountering errors.
[No sources needed since this section provides general guidance]
Common scenarios and resolutions:
Rate limit exceeded (429): Respect Retry-After and back off. Consider batching or reducing request frequency.
Unauthorized or forbidden: Verify authentication tokens and permissions.
Service unavailable: Check database connectivity and external service health.
Internal errors: Review logs and stack traces for root cause.
Error code reference (HTTP status mappings and guidance):
INVALID_REQUEST (400): Fix malformed requests.
UNAUTHORIZED (401): Provide valid credentials.
FORBIDDEN (403): Insufficient permissions.
NOT_FOUND (404): Resource does not exist.
CONFLICT (409): Resource conflict; retry with corrected state.
RATE_LIMITED (429): Back off and retry later.
INTERNAL_ERROR (500): Investigate server-side issues.
SERVICE_UNAVAILABLE (503): Dependency failure; retry after recovery.
Section sources
The project implements a consistent error response format and standardized rate limiting policies across both server types. The Telegram client’s retry/backoff and 429 handling, combined with service-level graceful degradation and robust logging, ensures resilient operation under load and transient failures. Adhering to the documented policies and using the provided patterns will maintain reliability and predictable behavior for both users and operators.